﻿namespace Tomato.Allure
{
	internal class ProjectItemImpl : Tomato.Allure.IProjectItem
	{
		private EnvDTE.ProjectItem m_instance;

		public Tomato.Allure.IDocument Document
		{
			get
			{
				if( m_instance.Document != null )
				{
					return new DocumentImpl( m_instance.Document );
				}

				return null;
			}
		}

		public int FileCount
		{
			get { return m_instance.FileCount; }
		}

		public string GetFileName( int index )
		{
			return m_instance.get_FileNames( (short)( index ) );
		}

		public string Name
		{
			get { return m_instance.Name; }
		}

		public Tomato.Allure.ProjectItemKind Kind
		{
			get
			{ 
				switch( m_instance.Kind )
				{
					case EnvDTE.Constants.vsProjectItemKindPhysicalFile: return Tomato.Allure.ProjectItemKind.PhysicalFile;
					case EnvDTE.Constants.vsProjectItemKindPhysicalFolder: return Tomato.Allure.ProjectItemKind.PhysicalFolder;
					case EnvDTE.Constants.vsProjectItemKindSolutionItems: return Tomato.Allure.ProjectItemKind.SolutionItems;
					case EnvDTE.Constants.vsProjectItemKindSubProject: return Tomato.Allure.ProjectItemKind.SubProject;
					case EnvDTE.Constants.vsProjectItemKindVirtualFolder: return Tomato.Allure.ProjectItemKind.VirtualFolder;
					case EnvDTE.Constants.vsProjectItemKindMisc: return Tomato.Allure.ProjectItemKind.Misc;
				}

				return Tomato.Allure.ProjectItemKind.None; 
			}
		}

		public bool IsSaved
		{
			get { return m_instance.Saved; }
		}

		public bool IsDirty
		{
			get { return m_instance.IsDirty; }
		}

		public Tomato.Allure.IProject SubProject
		{
			get 
			{
				if( m_instance.SubProject != null )
				{
					return new ProjectImpl( m_instance.SubProject );
				}

				return null;
			}
		}

		public Tomato.Allure.IProjectItems ProjectItems
		{
			get
			{
				if( m_instance.ProjectItems != null )
				{
					return new ProjectItemsImpl( m_instance.ProjectItems );
				}

				return null;
			}
		}

		public Tomato.Allure.IProject ContainingProject
		{
			get
			{
				if( m_instance.ContainingProject != null )
				{
					return new ProjectImpl( m_instance.ContainingProject );
				}

				return null;
			}
		}

		public ProjectItemImpl( EnvDTE.ProjectItem instance )
		{
			m_instance = instance;
		}

		public Tomato.Allure.IWindow Open()
		{
			EnvDTE.Window window = m_instance.Open( EnvDTE.Constants.vsViewKindPrimary );
			if( window != null )
			{
				return new WindowImpl( window );
			}

			return null;
		}
	}
}
